/**
 * 
 */
package simulation;

/**
 * @author aclayton
 *
 */
public class Circle {
	private Point center;
	private double radius;
	private Rect boundingBox;
	
	public Circle(Point center, double radius){
		this.center = center;
		this.radius = radius;
		this.boundingBox = new Rect(center.getX()-radius, center.getX()+radius, center.getY()+radius, center.getX()-radius);
	}
	
	public boolean intersectsCircle(Circle othercircle){
		return center.getDistance(othercircle.center) <= radius+othercircle.radius;
	}
	
	public boolean intersectsLineSeg(LineSeg seg){
		Rect lineBox = seg.getBoundingBox();
		if(!boundingBox.intersectsRect(lineBox))
			return false;
		
		
		Point perVector = seg.getVector().getPerpendicularVector().getUnit();
		Point p1 = perVector.multiply(radius).add(center);
		Point p2 = perVector.negate().multiply(radius).add(center);
		LineSeg perLine = new LineSeg(p1, p2);
		
		if(perLine.interectsSeg(seg))
			return true;
		
		if(center.getDistance(seg.getStart()) < radius)
			return true;
		
		if(center.getDistance(seg.getEnd()) < radius)
			return true;
				
		
		return false;
	}
	
	public boolean isPointInCircle(Point pt){
		double rad = pt.getDistance(center);
		
		if(rad <= radius)
			return true;
		
		return false;
	}

	/**
	 * @return the center
	 */
	public Point getCenter() {
		return center;
	}

	/**
	 * @return the radius
	 */
	public double getRadius() {
		return radius;
	}
	
	public Rect getBoundingBox(){
		return boundingBox;
	}
	
	public String toString(){
		return "Circle(" + center.getX() + "," + center.getY() + "," + radius + ")";
	}
	
	public static Circle createRandomInBox(Rect boundingbox, double radius){
		return new Circle(Point.getRandomPointInBox(boundingbox), radius);
	}
}
